#include <QString>

#include "mytcpsocket.h"
#include "protocol.h"
#include "operatedb.h"
#include "mytcpserver.h"

MyTcpSocket::MyTcpSocket()
{
    rh = new ReqHandler();
    connect(this, &QTcpSocket::readyRead   , this, &MyTcpSocket::recvMsg      );
    connect(this, &QTcpSocket::disconnected, this, &MyTcpSocket::clientOffline);
}

MyTcpSocket::~MyTcpSocket()
{
    delete rh;
}

// 发送消息
void MyTcpSocket::sendMsg(PDU *pdu)
{
    if(pdu == NULL) return;
    write((char*)pdu, pdu->uiPDULen);
    qDebug() << "发送消息->"
             << "消息类型:" << pdu->uiMsgType
             << "PDU长度:" << pdu->uiPDULen
             << "消息长度:" << pdu->uiMsgLen
             << "PDU数据1:" << pdu->caData
             << "PDU数据2:" << pdu->caData + 32
             << "消息内容:" << pdu->caMsg;
    free(pdu);
    pdu = NULL;
}

PDU *MyTcpSocket::readPDU()
{
    qDebug() << "readPDU 接收消息的长度:" << this->bytesAvailable();


    uint uiPDULen = 0;
    this->read((char*)&uiPDULen, sizeof(uint)); // 读协议总长度
    uint uiMsgLen = uiPDULen - sizeof(PDU);
    PDU* pdu      = mkPDU(0, uiMsgLen);
    this->read((char*)pdu + sizeof(uint), uiPDULen - sizeof(uint)); // 读取除了总长度以外的剩余长度的内容

    qDebug() << "\n接收消息->"
             << "消息类型:" << pdu->uiMsgType
             << "PDU长度:" << pdu->uiPDULen
             << "消息长度:" << pdu->uiMsgLen
             << "PDU数据1:" << pdu->caData
             << "PDU数据2:" << pdu->caData + 32
             << "消息内容:" << pdu->caMsg;
    return pdu;
}

PDU *MyTcpSocket::handleReq(PDU *pdu)
{
    PDU* resPdu = NULL;
    rh->pdu = pdu;
    switch(pdu->uiMsgType){
    case ENUM_MSG_TYPE_REGIST_REQUEST:
    {
        qDebug() << "\n注册请求>>";
        resPdu = rh->regist();
        break;
    }
    case ENUM_MSG_TYPE_LOGIN_REQUEST:
    {
        qDebug() << "\n登录请求>>";
        resPdu = rh->login(m_strLoginName);
        break;
    }
    case ENUM_MSG_TYPE_FIND_USER_REQUEST:
    {
        qDebug() << "\n查找用户请求>>";
        resPdu = rh->findUser();
        break;
    }
    case ENUM_MSG_TYPE_ONLINE_USER_REQUEST:
    {
        qDebug() << "\n在线用户请求>>";
        resPdu = rh->onlineUser();
        break;
    }
    case ENUM_MSG_TYPE_ADD_FRIEND_REQUEST:
    {
        qDebug() << "\n添加好友请求>>";
        resPdu = rh->addFriend();
        break;
    }
    case ENUM_MSG_TYPE_ADD_FRIEND_AGREE_REQUEST:
    {
        resPdu = rh->addFriendAgree();
        break;
    }
    case ENUM_MSG_TYPE_FLUSH_FRIEND_REQUEST:
    {
        resPdu = rh->flushFriend(m_strLoginName);
        break;
    }
    case ENUM_MSG_TYPE_DELETE_FRIEND_REQUEST:
    {
        qDebug() << "\n删除好友请求>>";
        resPdu = rh->delFriend();
        break;
    }
    case ENUM_MSG_TYPE_CHAT_REQUEST:
    {
        qDebug() << "\n聊天请求>>";
        resPdu = rh->chat();
        break;
    }
    case ENUM_MSG_TYPE_MKDIR_REQUEST:
    {
        qDebug() << "\n创建文件夹请求>>";
        resPdu = rh->mkDir();
        break;
    }
    default:
        break;
    }
    free(pdu) ;
    pdu = NULL;
    return resPdu;
}

// 接收消息
void MyTcpSocket::recvMsg()
{
    QByteArray data = this->readAll();
    buff.append(data);
    PDU* pdu = readPDU();
    PDU* resPdu = handleReq(pdu);
    sendMsg(resPdu);

}

void MyTcpSocket::clientOffline()
{
    OperateDB::getInstance().handleOffline(m_strLoginName.toStdString().c_str());
    emit offline(this);

}
